package com.motion.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.motion.dao.AdminCourseClassDao;
import com.motion.dto.*;
import com.motion.generator.entity.*;
import com.motion.result.Result;
import com.motion.result.ResultFactory;
import com.motion.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/admin")
@Api("web端管理员模块")
public class AdminController {
    @Autowired
    private CourseClassService courseClassService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;
    @Autowired
    private ActionService actionService;
    @Autowired
    private CourseLinkService courseLinkService;
    @Autowired
    private UserCourseService userCourseService;
    @Value("${default.actionImgs}")
    private String actionImgs;
    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ApplyService applyService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private OrderCopy1Service orderCopy1Service;
//    @Value("${default.orderCopy1Imgs}")
//    private String orderCopy1Imgs;
    //1.查
    //1.1 获取课程数据
    @RequestMapping(value = "/courseList", method = RequestMethod.GET)
    public Result courseList(@RequestParam Long page, @RequestParam Long size,
                             @RequestParam(defaultValue="2" ,required = false) int ifOnline,
                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime starTime ,
                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                             @RequestParam(defaultValue="",required = false) String searchStr){
        IPage<Course> courseIpage=courseService.getPageBystr(page,size,ifOnline,starTime,endTime,searchStr);
        AdminCourseTotalDto result=new AdminCourseTotalDto();
        result.setAdminCourseDtosList(courseService.CourseClassList2AdminCourseDtoList(courseIpage.getRecords()));
        result.setTotal(courseIpage.getTotal());
        return ResultFactory.buildSuccessResult(result);
    }
    //2.1课程分类获取
    @RequestMapping(value = "/courseClassList", method = RequestMethod.GET)
    public Result courseClassList(@RequestParam(defaultValue="0" ,required = false) int number,
                                  @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime starTime ,
                                  @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                                  @RequestParam(defaultValue="",required = false) String searchStr){
        List<CourseClass> courseClassList=courseClassService.courseClassesByStr(starTime,endTime,searchStr);
        HashMap<String, AdminCourseClassDto> hashMap = new HashMap<>();
        StringBuilder str =new StringBuilder();
        for(CourseClass item : courseClassList){
            String name = item.getClassName();
            String det=item.getClassValue();
            if(!hashMap.containsKey(name)){
                AdminCourseClassDto adminCourseClassDto=new AdminCourseClassDto();
                adminCourseClassDto.setClassCreateTime(item.getCreateTime());
                adminCourseClassDto.setClassName(item.getClassName());
                adminCourseClassDto.setClassNum(0);
                adminCourseClassDto.setClassUrl(item.getClassUrl());
                adminCourseClassDto.setCourseClassId(item.getCourseClassId());
                adminCourseClassDto.setClassCreator(userService.getUserById(item.getUploadId()).getNickName());
                adminCourseClassDto.setClassValue("");
                adminCourseClassDto.setCourseClassList(new ArrayList<>());
                adminCourseClassDto.setIdList(new ArrayList<>());
                hashMap.put(name, adminCourseClassDto);
            }
            if (det.equals("全部")){
                AdminCourseClassDto admincourseClassDto = hashMap.get(name);
                admincourseClassDto.setClassUrl(item.getClassUrl());
                admincourseClassDto.setCourseClassId(item.getCourseClassId());
                admincourseClassDto.setClassCreateTime(item.getCreateTime());
                hashMap.put(name, admincourseClassDto);
            }else {
                AdminCourseClassDto admincourseClassDto = hashMap.get(name);
                str = new StringBuilder(admincourseClassDto.getClassValue());
                str.append(item.getClassValue()).append(",");
                admincourseClassDto.setClassValue(str.toString());
                admincourseClassDto.setClassNum(admincourseClassDto.getClassNum() + 1);
                admincourseClassDto.getIdList().add(item.getCourseClassId());
                admincourseClassDto.getCourseClassList().add(item);
                hashMap.put(name, admincourseClassDto);
            }
        }
        List<AdminCourseClassDto> result = new LinkedList<>();
        for(String key: hashMap.keySet()){
            if(number!=0) {
                if (hashMap.get(key).getClassNum() == number)
                    result.add(hashMap.get(key));
            }
            else
                result.add(hashMap.get(key));
        }
        AdminCourseClassTotalDto adminCourseClassTotalDto = new AdminCourseClassTotalDto();
        adminCourseClassTotalDto.setAdminCourseClassDtoList(result);
        adminCourseClassTotalDto.setTotal(courseClassList.size());
        return ResultFactory.buildSuccessResult(adminCourseClassTotalDto);
    }
    //3.1   .获取动作数据
    @RequestMapping(value = "/actionList", method = RequestMethod.GET)
    public Result actionList(@RequestParam Long page,@RequestParam Long size,
                             @RequestParam(defaultValue="2",required = false) int type,
                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime starTime ,
                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                             @RequestParam(defaultValue="",required = false) String searchStr){
        IPage<Action> actionIPage=actionService.getPageByStrall(page, size,type,starTime,endTime,searchStr);
        List<Action> actions=actionIPage.getRecords();
        AdminActionTotalDto result = new AdminActionTotalDto();
        result.setAdminActionDtoList(actionService.actionList2AdminActionDtoLsit(actions));
        result.setTotal(actionIPage.getTotal());
        return ResultFactory.buildSuccessResult(result);
    }
    //4.1  获取用户数据
    @RequestMapping(value = "/userManagerList", method = RequestMethod.GET)
    public Result userManagerList(@RequestParam Long page,@RequestParam Long size,
                                  @RequestParam(defaultValue="2",required = false) int ifban,
                                  @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime starTime ,
                                  @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                                  @RequestParam(defaultValue="",required = false) String searchStr){
        IPage<User> userIPage=userService.getUserByStr(page, size,ifban,starTime,endTime,searchStr);
        AdminUserDto result=new AdminUserDto();
        result.setUserList(userIPage.getRecords());
        result.setTotal(userIPage.getTotal());
        return ResultFactory.buildSuccessResult(result);
    }
    //0.1登录

    @Data
    public class admintoken {
        String token = "123";
    }
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public Result login(){
        admintoken first=new admintoken();
        return ResultFactory.buildSuccessResult(first);
    }
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public Result login_admin(String token){
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        return ResultFactory.buildSuccessResult(userService.getUserById(userId));
    }
    @RequestMapping(value = "/user/info", method = RequestMethod.GET)
    public Result info(){
        loginDto ok=new loginDto();
        ok.setName("小胖");
        ok.setUrl("www.baidu.com");
        return ResultFactory.buildSuccessResult(ok);
    }

    //2、增 、未加token
    //2.1增课程
    @RequestMapping(value = "/insertCourse", method = RequestMethod.POST)
    public Result insertCourse(@RequestBody insertCourseDto course ){
        String token=course.getToken();
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        course.setUploaderId(userId);
        course.setCreateTime(LocalDateTime.now());
        course.setHits(0);
        course.setCollections(0);
        course.setTargetAge("3-5岁");
        String actionStr=course.getActions();
        String[] actionList=actionStr.split(",");
        course.setActions(StringUtils.join(";",actionList));
        String countStr=course.getCounts();
        String[] countList=countStr.split(",");
        course.setCounts(StringUtils.join(";",countList));
        Long time=0L;
        if(!actionList[0].equals("")){
            for (int t=0;t<actionList.length;t++){
                time=time+actionService.getActionById(Long.valueOf(actionList[t])).getDuration()*Integer.valueOf(countList[t]);
            }
        }
        int h=time.intValue()/3600;
        int m=(time.intValue()%3600)/60;
        int s=(time.intValue()%3600)%60;
        LocalTime duration=LocalTime.of(h,m,s);
        course.setDuration(duration);
        Course result=courseService.insertCourse(course);
        String[] labelList;
        if(!course.getLabels().equals("")) {
            labelList = course.getLabels().split(",");
            for (String item : labelList) {
                courseLinkService.insertCourse(result.getCourseId(), Long.valueOf(item));
            }
        }
        return ResultFactory.buildSuccessResult(result);
    }
    //2.2。1增 分类
    @RequestMapping(value = "/insertCourseClass", method = RequestMethod.POST)
    public Result insertCourseClass(@RequestBody insertCourseClassDto courseClass ) {
        String token=courseClass.getToken();
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        courseClass.setUploadId(userId);
        int tem=courseClassService.ifexist(courseClass);
        switch (tem){
            case 0:
                return ResultFactory.buildSuccessResult(courseClassService.insertCourseClass(courseClass));
            case 1:
                return ResultFactory.buildFailResult("分类组不存在");
            case 2:
                return ResultFactory.buildFailResult("分类已存在");
            default :
                return ResultFactory.buildFailResult("未知错误");
        }
    }
    //2.2.2增 分类组
    @RequestMapping(value = "/insertCourseClassList", method = RequestMethod.POST)
    public Result insertCourseClassList(@RequestBody insertCourseClassDto courseClass ) {
        String token=courseClass.getToken();
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        courseClass.setClassValue("全部");
        courseClass.setUploadId(userId);
        int tem=courseClassService.ifexist(courseClass);
        switch (tem){
            case 1:
                return ResultFactory.buildSuccessResult(courseClassService.insertCourseClass(courseClass));
            case 2:
                return ResultFactory.buildFailResult("分类组已存在");
            default :
                return ResultFactory.buildFailResult("未知错误");
        }
    }
    @RequestMapping(value = "/insertAction", method = RequestMethod.POST)
    public Result insertAction(@RequestBody insertActionDto action ) {
        String token=action.getToken();
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        if (action.getActionImgs().equals(""))
            action.setActionImgs(actionImgs);
        action.setUploadId(userId);
        action.setCreateTime(LocalDateTime.now());
        action.setTimeStamp(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        Action result=actionService.insertActionDto(action);
        return ResultFactory.buildSuccessResult(result);
    }
    //删
    //3.1 删课程
    @RequestMapping(value = "/deleteCourse", method = RequestMethod.GET)
    public Result deleteCourse(@RequestParam Long courseId,@RequestParam String token){
        courseService.deleteCourse(courseId);
        List<CourseLink> courseLinks=courseLinkService.getCourseLinkBycourse(courseId);
        for (CourseLink item:courseLinks){
            courseLinkService.delectCourseLink(item.getCourseLinkId());
        }
        List<UserCourse> courseList=userCourseService.course2userCourse(courseId);
        List<Long> userCourseList=courseList.stream().map(UserCourse::getCourseId).collect(Collectors.toList());
        userCourseService.delectByList(userCourseList);
        return ResultFactory.buildSuccessResult(courseId);
    }
    //3.2 删课分类 分情况
    @RequestMapping(value = "/deleteCourseClass", method = RequestMethod.GET)
    public Result deleteCourseClass(@RequestParam Long courseClassId,@RequestParam String token){
        if(courseClassService.deleteCourseClass(courseClassId)){
            return ResultFactory.buildSuccessResult(courseClassId);
        }else {
            return ResultFactory.buildFailResult("id错误");
        }

    }
    //3.3 删课程
    @RequestMapping(value = "/deleteAction", method = RequestMethod.GET)
    public Result deleteAction(@RequestParam Long actionId,@RequestParam String token){
        actionService.deleteAction(actionId);
        //删除关联课程的动作
        List<Course> courseList=actionService.getDelectActionToCourse(actionId);
        for (Course item:courseList){
            List<String> count= Arrays.asList(item.getCounts().split(";"));
            List<String> action= Arrays.asList(item.getActions().split(";"));
            int t=0;
            while(t<action.size()){
                if (action.get(t).equals(String.valueOf(actionId))){
                    action.remove(t);
                    count.remove(t);
                    t--;
                }
                t++;
            }
            item.setActions(org.apache.commons.lang3.StringUtils.join(action,";"));
            item.setCounts(org.apache.commons.lang3.StringUtils.join(count,";"));
            courseService.updataCourse(item);
        }
        return ResultFactory.buildSuccessResult(actionId);
    }
    //3.4 禁用用户，
    @RequestMapping(value = "/banUser", method = RequestMethod.GET)
    public Result banUser(@RequestParam Long userId,@RequestParam String token){
        User user=userService.getUserById(userId);
        user.setType(0);
        userService.updateUser(user);
        return ResultFactory.buildSuccessResult(user);
    }
    //4.改
    //4.1改课程
    @RequestMapping(value = "/updataCourse", method = RequestMethod.POST)
    public Result updataCourse(@RequestBody insertCourseDto course){
        String actionStr=course.getActions();
        String[] actionList=actionStr.split(",");
        course.setActions(StringUtils.join(";",actionList));
        String countStr=course.getCounts();
        String[] countList=countStr.split(",");
        course.setCounts(StringUtils.join(";",countList));

        //删除标签
        List<CourseLink> courseLinks=courseLinkService.getCourseLinkBycourse(course.getCourseId());
        for (CourseLink item:courseLinks){
            courseLinkService.delectCourseLink(item.getCourseLinkId());
        }
        //添加标签
        String[] labelList;
        if(!course.getLabels().equals("")) {
            labelList = course.getLabels().split(",");
            for (String item : labelList) {
                courseLinkService.insertCourse(course.getCourseId(), Long.valueOf(item));
            }
        }
        Course course_old=courseService.updataCourse(course);
        return ResultFactory.buildSuccessResult(course_old);
    }
    //4.2改动作
    @RequestMapping(value = "/updataAction", method = RequestMethod.POST)
    public Result updataAction(@RequestBody Action action){
        action.setTimeStamp(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        return ResultFactory.buildSuccessResult(actionService.updataAction(action));
    }
    //4.3改分类动作
    @RequestMapping(value = "/updataCourseClass", method = RequestMethod.POST)
    public Result updataCourseClassList(@RequestBody AdminCourseClassDao adminCourseClassDao){
        List<CourseClass> tem=adminCourseClassDao.getCourseClassList();
        CourseClass courseClass=new CourseClass();
        BeanUtils.copyProperties(adminCourseClassDao,courseClass);
        courseClassService.updataCourseClass(courseClass);
        for(CourseClass item:tem){
            item.setClassName(courseClass.getClassName());
            courseClassService.updataCourseClass(item);
        }
        return ResultFactory.buildSuccessResult("成功");
    }
    //单个修改，方便后续使用
    @RequestMapping(value = "/updataCourseClassByOne", method = RequestMethod.GET)
    public Result updataCourseClassByOne(@RequestParam CourseClass courseClass){
        return ResultFactory.buildSuccessResult(courseClassService.updataCourseClass(courseClass));
    }
    //课程动作获取数据
    @RequestMapping(value = "/getAction", method = RequestMethod.GET)
    public Result getAction(@RequestParam String actionStr,@RequestParam Long page,@RequestParam Long size){
        //str为空则代表全部
        if(actionStr.equals("")){
            return ResultFactory.buildSuccessResult(actionService.getPageAll(page,size).getRecords());
        }else{
            return ResultFactory.buildSuccessResult(actionService.getPageByStr(actionStr,page,size).getRecords());
        }
    }
    //查.
    @RequestMapping(value = "/saveUserdata", method = RequestMethod.POST)
    public Result setUserdata(@RequestBody User user){
        System.out.println(user.getHeadPortrait());
        if(StpUtil.getLoginIdByToken(user.getToken())==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(user.getToken())));
        user.setUserId(userId);
        userService.updateUser(user);
        return ResultFactory.buildSuccessResult(userService.getUserById(user.getUserId()));
    }
    //动作删除关联提示
    @RequestMapping(value = "/getDelectActionToCourse", method = RequestMethod.GET)
    public Result getDelectActionToCourse(@RequestParam Long actionId){
        return ResultFactory.buildSuccessResult(actionService.getDelectActionToCourse(actionId));
    }

    //获取全部反馈
    @RequestMapping(value = "/getUserFeedback", method = RequestMethod.GET)
    public Result getUserFeedback(@RequestParam Long page,@RequestParam Long size,
                                  @RequestParam (defaultValue="2",required = false) Integer value_type,
                                  @RequestParam (defaultValue="",required = false) String input,
                                  @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime starTime ,
                                  @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        AdminFeedbackDto result=new AdminFeedbackDto();
        IPage<Feedback> feed=feedbackService.getAllFeedbackPage(page,size,value_type,input,starTime,endTime);
        result.setFeedbackList(feed.getRecords());
        result.setTotal(feed.getTotal());
        return ResultFactory.buildSuccessResult(result);
    }

    //回复
    @RequestMapping(value = "/saveUserFeedback", method = RequestMethod.GET)
    public Result saveUserFeedback(@RequestParam Long feedbackId,@RequestParam String answer){
        Feedback feedback=feedbackService.getFeedbackById(feedbackId);
        feedback.setFeedbackId(feedbackId);
        feedback.setReply(answer);
        if(feedback.getReply().length()>0){
            feedback.setIfSolve(1);
        }else{
            feedback.setIfSolve(0);
        }
        feedbackService.updateFeedback(feedback);
        return ResultFactory.buildSuccessResult(feedback);
    }

    //文章模块
    //文章筛选获取
    @RequestMapping(value = "/articleList", method = RequestMethod.GET)
    public Result articleList(@RequestParam Long page, @RequestParam Long size,
                             @RequestParam(defaultValue="2" ,required = false) int ifRelease,
                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime starTime ,
                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                             @RequestParam(defaultValue="",required = false) String searchStr){
        List<Article> articleList=articleService.articleList(page,size,ifRelease,starTime,endTime,searchStr);
        return ResultFactory.buildSuccessResult(articleService.Article2ArticleDto(articleList));
    }
    //删
    @RequestMapping(value = "/delectArticle", method = RequestMethod.GET)
    public Result delectArticle(@RequestParam String token,@RequestParam Long articleId){
        return ResultFactory.buildSuccessResult(actionService.deleteAction(articleId));
    }
    //改
    @RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
    public Result updateArticle(@RequestBody ArticleDto articleDto){
        if(StpUtil.getLoginIdByToken(articleDto.getToken())==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId = Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(articleDto.getToken())));
        articleService.updateArticle(articleDto);
        return ResultFactory.buildSuccessResult(articleDto);
    }

    //认证申请模块
    //查
    @RequestMapping(value = "/getApply",method=RequestMethod.GET)
    public Result getApply(@RequestParam Long page,@RequestParam Long size){
        //获取数据
        List<Apply> ref=applyService.cheak(page, size);
        //返还数据
        return ResultFactory.buildSuccessResult(ref);
    }

    //订单管理模块
    //查

    @RequestMapping(value = "/getOrder",method=RequestMethod.GET)
    @ApiOperation(value = "订单查",notes = "根据手机号获取短信验证码")
    public Result getOrder(@RequestParam Long page,@RequestParam Long size){
        //获取数据
        List<OrderCopy1> ref=orderCopy1Service.cheak(page, size);
        //返还数据
        return ResultFactory.buildSuccessResult(ref);
    }

    //增
    @RequestMapping(value = "/insertOrder", method = RequestMethod.POST)
    public Result insertOrderCopy1(@RequestBody insertOrderCopy1Dto orderCopy1 ) {
//        String token=orderCopy1.getToken();
//        if(StpUtil.getLoginIdByToken(token)==null){
//            return ResultFactory.buildResultTokenError("token无效");
//        }
//        Long userId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        String waybillNumber =orderCopy1.getWaybillNumber();
        String address =orderCopy1.getAddress();
        String state =orderCopy1.getState();
        String goodsName =orderCopy1.getGoodsName();
        Integer freight=orderCopy1.getFreight();
        LocalDateTime time=orderCopy1.getTime();
        Integer price=orderCopy1.getPrice();

        OrderCopy1 orderCopy11=new OrderCopy1();

        orderCopy11.setWaybillNumber(waybillNumber);
        orderCopy11.setAddress(address);
        orderCopy11.setState(state);
        orderCopy11.setGoodsName(goodsName);
        orderCopy11.setTime(time);
        orderCopy11.setFreight(freight);
        orderCopy11.setPrice(price);

        OrderCopy1 result=orderCopy1Service.insertOrderCopy1Dto(orderCopy11);
        return ResultFactory.buildSuccessResult(result);
    }

    //删
    @RequestMapping(value = "/delectOrder", method = RequestMethod.GET)
    public Result delectOrderCopy1(@RequestParam String token,@RequestParam Long orderCopy1Id){
        return ResultFactory.buildSuccessResult(orderCopy1Service.deleteOrderCopy1(orderCopy1Id));
    }
    //商品
    //查
    @RequestMapping(value = "/getGoods",method = RequestMethod.GET)
    public Result getGoods(@RequestParam Long page,@RequestParam Long size){
        List<Goods> ref =  goodsService.cheak(page, size);
        return ResultFactory.buildSuccessResult(ref);
    }
    //增
    @RequestMapping(value = "/insertGoods",method = RequestMethod.POST)
    public Result insertGoods(@RequestBody insertGoodsDto goods){
        String token=goods.getToken();
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long goodsId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        String goodsName = goods.getGoodsName();
        String goodsUrl = goods.getGoodsUrl();
        Integer goodsPrice = goods.getPrice();
        String goodsType = goods.getType();
        Integer goodsFreight = goods.getFreight();
        Integer goodsInventory = goods.getInventory();
        Integer goodsExchange = goods.getExchange();

        goods.setGoodsId(goodsId);
        goods.setGoodsName(goodsName);
        goods.setGoodsUrl(goodsUrl);
        goods.setPrice(goodsPrice);
        goods.setType(goodsType);
        goods.setFreight(goodsFreight);
        goods.setInventory(goodsInventory);
        goods.setExchange(goodsExchange);

        Goods result = goodsService.insertGoodsDto(goods);
        return ResultFactory.buildSuccessResult(result);
    }
    //删
    @RequestMapping(value = "/deleteGoods",method = RequestMethod.GET)
    public Result deleteGoods(@RequestParam String token,@RequestParam Long goodsId){
        return ResultFactory.buildSuccessResult(goodsService.deleteGoods(goodsId));
    }
    //改
    @RequestMapping(value = "/updateGoods",method = RequestMethod.POST)
    public Result updateGoods(@RequestBody GoodsDto goodsDto){
        if(StpUtil.getLoginIdByToken(goodsDto.getToken())==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId = Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(goodsDto.getToken())));
        goodsService.updateGoods(goodsDto);
        return ResultFactory.buildSuccessResult(goodsDto);
    }

    //导航模块
    //查
    //twe
    @RequestMapping(value = "/getNavigation",method = RequestMethod.GET)
    public Result getNavigation(@RequestParam Long page,@RequestParam Long size){
        List<Navigation> ref = navigationService.cheak(page,size);
        return ResultFactory.buildSuccessResult(ref);
    }
    //删
    @RequestMapping(value = "/delectNavigation", method = RequestMethod.GET)
    public Result delectNavigation(@RequestParam String token,@RequestParam Long navigationId){
        return ResultFactory.buildSuccessResult(navigationService.delectNavigation(navigationId));
    }
    //增
    @RequestMapping(value = "/insertNavigation", method = RequestMethod.POST)
    public Result insertNavigation(@RequestBody insertNavigationDto navigation ) {
//        String token=navigation.getToken();
//        if(StpUtil.getLoginIdByToken(token)==null){
//            return ResultFactory.buildResultTokenError("token无效");
//        }
//        Long navigationId=Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
//        navigation.setNavigationId(navigationId);
        String userName = navigation.getName();
        navigation.setName(userName);
        String imgUrl = navigation.getImgUrl();
        navigation.setImgUrl(imgUrl);
        String content = navigation.getContent();
        navigation.setContent(content);
        LocalDateTime dateTime = navigation.getTime();
        navigation.setTime(dateTime);
        Navigation result=navigationService.insertNavigationDto(navigation);
        return ResultFactory.buildSuccessResult(result);
    }
    //改
    @RequestMapping(value = "/updateNavigation", method = RequestMethod.POST)
    public  Result updateNavigation(@RequestParam NavigationDto navigationDto){
//        if(StpUtil.getLoginIdByToken(navigationDto.getToken() == null)){
//            return ResultFactory.buildResultTokenError("token无效");
//        }
        String token=navigationDto.getToken();
        Long userId = Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(navigationDto.getToken)));
        navigationService.updateNavigation(navigationDto);
        return ResultFactory.buildSuccessResult(navigationDto);
    }
}
